<template>
  <div class="app-container">
    <div>
      <el-collapse v-model="activeNames" @change="handleChange">
        <el-collapse-item title="添加" name="4">
          <div style="width: 30%">
            <el-form ref="form" :model="sort" label-width="80px">
              <el-form-item label="导航名">
                <el-input v-model="sort.sortName"></el-input>
              </el-form-item>
              <el-form-item label="描述">
                <el-input v-model="sort.sortDescribe" type="textarea"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="addSubmit">添加</el-button>
                <el-button @click="reset">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      stripe
    >
      <!--      @row-click="getUser"-->
      <el-table-column align="center" label="id" width="95">
        <template slot-scope="scope">
          {{ scope.row.sortId }}
        </template>
      </el-table-column>
      <el-table-column label="导航名">
        <template slot-scope="scope">
          {{ scope.row.sortName }}
        </template>
      </el-table-column>
      <el-table-column label="描述">
        <template slot-scope="scope">
          {{ scope.row.sortDescribe }}
        </template>
      </el-table-column>
      <el-table-column label="创建日期">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isShow"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value=1
            :inactive-value=0
            active-text="启用"
            inactive-text="关闭"
            @change=change(scope.row.sortId)>
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="200"
      >
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" plain @click="getDetail(scope.row)" />
          <el-button type="danger" size="mini" icon="el-icon-delete" plain @click="deleteById(scope.row.sortId)" />
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-drawer
      title="我是标题"
      :visible.sync="drawer"
      :with-header="true"
    >
      <template>
        <el-form ref="form" :model="sortNav" label-width="80px">
          <el-form-item label="导航名">
            <el-input v-model="sortNav.sortName" />
          </el-form-item>
          <el-form-item label="描述">
            <el-input type="textarea" v-model="sortNav.sortDescribe"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">发布</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>
      </template>
    </el-drawer>
  </div>
</template>

<script>
import { addSortNav, getSortNavList, updateSortNav, updateSortNavIsShow, deleteSortNavIsShow } from '@/api/sort'

export default {
  name: 'SortNavigation',
  data() {
    return {
      list: null,
      listLoading: false,
      sortNavList: {
        sortId: 0,
        sortName: '',
        createTime: '',
        isShow: 0,
        sortDescribe: ''
      },
      sortNav: {
        sortId: 0,
        sortName: '',
        createTime: '',
        isShow: 1,
        sortDescribe: ''
      },
      pageNum: 1,
      total: 0,
      pageSize: 10,
      drawer: false,
      activeNames: ['1'],
      sort: {
        sortName: '',
        sortDescribe: ''
      }
    }
  },
  created() {
    this.findSortNavList()
  },
  methods: {
    handleChange(val) {
      console.log(val)
    },
    findSortNavList() {
      getSortNavList({ pageNum: this.pageNum, pageSize: this.pageSize }).then(response => {
        this.total = response.total
        this.list = response.data
        this.listLoading = false
      })
    },
    handleSizeChange(size) {
      this.pageSize = size
      this.fetchData()
    },
    handleCurrentChange(page) {
      this.pageNum = page
      this.fetchData()
    },
    onSubmit: function() {
      this.$confirm('是否修改?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateSortNav({
          sortId: this.sortNav.sortId,
          sortName: this.sortNav.sortName,
          sortDescribe: this.sortNav.sortDescribe
        }).then(res => {
          this.$message.success('成功')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        })
      })
    },
    getDetail(sortNav) {
      this.drawer = true
      console.log(sortNav)
      this.sortNav = sortNav
    },
    deleteById(sortId) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteSortNavIsShow({ sortId: sortId })
          .then(res => {
            this.findSortNavList()
            this.$message.success(res.message)
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        })
      })
    },
    change(sortId) {
      updateSortNavIsShow({ sortId: sortId }).then(res => {
        this.$message.success(res.message)
      })
      // this.findSortNavList()
    },
    addSubmit() {
      this.$confirm('是否添加?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        addSortNav(this.sort).then(res => {
          this.reset()
          this.$message.success(res.message)
          this.findSortNavList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        })
      })
    },
    reset() {
      this.sort = {
        sortName: '',
        sortDescribe: ''
      }
    }
  }
}
</script>

<style scoped>

</style>
